﻿using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Threading;
using Xcj.Mes.WholeLine.DataBase.Entity;

namespace Xcj.Mes.WholeLine.ZH.HG.Shell.Views.Main
{
    /// <summary>
    /// CaptionBarView.xaml 的交互逻辑
    /// </summary>
    public partial class CaptionBarView : UserControl
    {
        private bool IsWindowMoving = false;

        public CaptionBarView()
        {
            InitializeComponent();
            this.MouseMove += new MouseEventHandler(OnMouseMove);
        }

        private void OnMouseMove(object sender, MouseEventArgs e)
        {
            if (System.Windows.Application.Current?.Dispatcher == null)
                return;

            if ((e.LeftButton == MouseButtonState.Released) && IsWindowMoving)
            {
                IsWindowMoving = false;

                ThreadPool.QueueUserWorkItem(delegate
                {
                    SynchronizationContext.SetSynchronizationContext(new DispatcherSynchronizationContext(Application.Current?.Dispatcher));
                    SynchronizationContext.Current?.Post(p =>
                    {
                        try
                        {
                            if ((Application.Current?.MainWindow?.WindowState ?? WindowState.Maximized) != WindowState.Maximized)
                            {
                                Application.Current.MainWindow.WindowState = WindowState.Maximized;
                                Application.Current.MainWindow.WindowStartupLocation = WindowStartupLocation.CenterScreen;
                            }
                        }
                        catch { }
                    }, null);
                });
            }
        }

        public void OnPreviewMouseLeftButtonDown(object source, MouseButtonEventArgs e)
        {
            if (System.Windows.Application.Current?.Dispatcher == null)
                return;

            ThreadPool.QueueUserWorkItem(delegate
            {
                SynchronizationContext.SetSynchronizationContext(new DispatcherSynchronizationContext(Application.Current?.Dispatcher));
                SynchronizationContext.Current?.Post(p =>
                {
                    try
                    {
                        if ((Application.Current?.MainWindow?.WindowState ?? WindowState.Normal) != WindowState.Normal)
                        {
                            Application.Current.MainWindow.WindowState = WindowState.Normal;
                            Application.Current.MainWindow.Left   = 0;
                            Application.Current.MainWindow.Top    = 0;
                            Application.Current.MainWindow.Width  = SystemParameters.PrimaryScreenWidth;
                            Application.Current.MainWindow.Height = SystemParameters.PrimaryScreenHeight;
                            Application.Current.MainWindow.WindowStartupLocation = WindowStartupLocation.CenterScreen;
                        }

                        IsWindowMoving = true;

                        Application.Current?.MainWindow?.DragMove();
                    }
                    catch { }
                }, null);
            });
        }

        public void OnExit(object source, RoutedEventArgs e)
        {
            ThreadPool.QueueUserWorkItem(delegate
            {
                SynchronizationContext.SetSynchronizationContext(new DispatcherSynchronizationContext(Application.Current?.Dispatcher));
                SynchronizationContext.Current?.Post(p =>
                {
                    try { Application.Current?.MainWindow?.Close(); } catch { }
                }, null);
            });
        }

        public void OnMinimized(object source, RoutedEventArgs e)
        {
            ThreadPool.QueueUserWorkItem(delegate
            {
                SynchronizationContext.SetSynchronizationContext(new DispatcherSynchronizationContext(Application.Current?.Dispatcher));
                SynchronizationContext.Current?.Post(p =>
                {
                    try { Application.Current.MainWindow.WindowState = WindowState.Minimized; } catch { }
                }, null);
            });
        }

        public void OnHide(object source, RoutedEventArgs e)
        {
            ThreadPool.QueueUserWorkItem(delegate
            {
                SynchronizationContext.SetSynchronizationContext(new DispatcherSynchronizationContext(Application.Current?.Dispatcher));
                SynchronizationContext.Current?.Post(p =>
                {
                    try { Application.Current.MainWindow.Visibility = Visibility.Hidden; } catch { }
                }, null);
            });
        }
    }
}
